package com.example.administrator.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31.
 */

public class OneFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener {

    GridView gridView;
    List<Book> list = new ArrayList<>();
    MyBookAdapter myBookAdapter;

    private MyApplication app1;
    DbManager db;
    private MyApplication app2;

    private EditText query_text;
    private ImageView query_imgge;
    private ImageView sort;
    private TextView all;

    View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_one,null);
        gridView = (GridView)view.findViewById(R.id.tab1_book_grid);
        app1 = new MyApplication();

        getData();
        myBookAdapter = new MyBookAdapter(getActivity(),list);
        gridView.setAdapter(myBookAdapter);
        gridView.setOnItemClickListener(this);

        query_imgge = (ImageView)view.findViewById(R.id.tab1_query_image);
        query_text = (EditText)view.findViewById(R.id.tab1_query_text);
        query_imgge.setOnClickListener(this);

        sort = (ImageView)view.findViewById(R.id.tab1_sort);
        sort.setOnClickListener(this);

        all = (TextView)view.findViewById(R.id.tab1_query_all);
        all.setOnClickListener(this);

        return view;
    }

    public void getData() {
        db = x.getDb(app1.getDaoConfig());
        try {
            List<Book> books = db.selector(Book.class).findAll();
            if(books.size()!=0){
                list = books;
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent book_detail = new Intent(getActivity(),BookDetailActivity.class);
        Book book = list.get(i);
        book_detail.putExtra("book",book);
        startActivity(book_detail);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tab1_query_image:
                if(query_text.getText().toString().equals("")){
                    Toast.makeText(getActivity(),"搜索内容不能为空",Toast.LENGTH_SHORT).show();
                }else{
                   db  = x.getDb(app1.getDaoConfig());
                    try {
                        List<Book> books = db.selector(Book.class).where("title","like","%"+query_text.getText().toString()+"%").findAll();
                        if(books.size()!=0){
                            list.clear();
                            for (Book book:books){
                                list.add(book);
                            }
                            myBookAdapter.notifyDataSetChanged();
                        }else{
                            Toast.makeText(getActivity(),"没有相关内容",Toast.LENGTH_SHORT).show();
                        }
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.tab1_query_all:
                db = x.getDb(app1.getDaoConfig());
                try {
                    List<Book> books = db.selector(Book.class).findAll();
                    if(books.size()!=0){
                        list.clear();
                        for (Book book:books){
                            list.add(book);
                        }
                        myBookAdapter.notifyDataSetChanged();
                    }else{
                        Toast.makeText(getActivity(),"没有相关内容",Toast.LENGTH_SHORT).show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;

            case R.id.tab1_sort:
                Toast.makeText(getActivity(),"分类",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
